﻿<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
body{font-size:12px; margin:0px; text-align:center}
td{font-size:12px;}
</style>
<?php


/**
 * 数据采集
 */
header('Content-type: text/html; charset=utf-8');

include_once 'module/simple_html_dom.php';

date_default_timezone_set("Asia/Shanghai");
$matchId = $_GET["matchId"];
$homeTeam = @ $_GET["homeTeam"];
$visitTeam = @ $_GET["visitTeam"];
$matchTime = @ $_GET["matchTime"];
$matchCategory = @ $_GET["matchCategory"];

if ($matchId == "") {
	return;
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://match.sports.sina.com.cn/iframe/livecast/soccer_gundongshipin.php?id=" . $matchId . "?" . rand());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
$matchVideo = curl_exec($ch);
curl_close($ch);
$matchVideo = str_get_html($matchVideo);
$videoList = $matchVideo->find('div[class=scrollPic] p');

//连接数据库
$config = require 'conf/config.ini.php'; //加载公用config.ini.php
$conn = mysqli_connect($config["DB_HOST"], $config["DB_USER"], $config["DB_PWD"], $config["DB_NAME"]);

//先取得当前数据库中的数据
$sql = "select matchTime, videoId from live_video where videoCategory = '" . $matchCategory . "'";

$result = mysqli_query($conn, $sql);

$sql = "INSERT INTO live_video (matchTime, videoId, videoTitle, videoImgUrl, videoLength, videoUrl, homeTeam, visitTeam, videoCategory, updateDate) VALUES";

$i = 0;
$j = 0;
$videoTempId = NULL;
foreach ($videoList as $video) {
	if (strpos($video->find('a', 0)->href, ".html")) {

		$videoImgUrl = $video->find('a img', 0)->src;
		$videoId = substr($videoImgUrl, strripos($videoImgUrl, "/") + 1, 10);
		if ($videoTempId != $videoId) {
			$i = $i +1;

			$videoTempId = $videoId;

			$insert = true;
			mysqli_data_seek($result, 0);
			while ($row_array = mysqli_fetch_array($result, MYSQL_ASSOC)) {
				if ($row_array["videoId"] == $videoId) {
					$insert = false;
					break;
				}
			}
			if ($insert) {
				$j = $j +1;
				$matchTime = $video->find('a', 0)->href;
				$matchTime = substr($matchTime, 0, strripos($matchTime, "/"));
				$matchTime = substr($matchTime, -10);
				$videoTitle = iconv("gbk", "utf-8", $video->find('a', 1)->innertext);
				$videoUrl = "http://you.video.sina.com.cn/api/sinawebApi/outplayrefer.php/vid=" . $videoId . "/s.swf";
				$sql .= " ('" . $matchTime . "', '" . $videoId . "', '" . $videoTitle . "', '" . $videoImgUrl . "', '', '" . $videoUrl . "', '" . $homeTeam . "', '" . $visitTeam . "', '" . $matchCategory . "', now()),";
			}
		}
	}
}

$sql = " SET NAMES utf8;" . substr($sql, 0, strlen($sql) - 1) . ";";

if ($i > 0) {
	//插入比赛比分
	mysqli_multi_query($conn, $sql);
}

echo "共采集" . $i . "段视频";
?>